package com.xxx.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxx.course.entity.EduChapter;
import com.xxx.course.entity.EduCourse;
import com.xxx.course.entity.EduVideo;
import com.xxx.course.entity.course.*;
import com.xxx.course.mapper.EduCourseMapper;
import com.xxx.course.service.EduChapterService;
import com.xxx.course.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.course.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author chn
 * @since 2022-05-29
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    @Override
    public List<Course> getCourse(){
        List<EduCourse> eduCourses = baseMapper.selectList(null);
        List<EduVideo> eduVideos = eduVideoService.list(null);
        List<EduChapter> eduChapters = eduChapterService.list(null);
        List<Course> courses=new ArrayList<>();
        for (EduCourse eduCourse : eduCourses) {
            Course course = new Course();
            BeanUtils.copyProperties(eduCourse,course);     //课程
            courses.add(course);

            List<Chapter> chapters=new ArrayList<>();
            for (EduChapter eduChapter : eduChapters) {//获取章节
                if (eduChapter.getCourseId().equals(eduCourse.getId())){
                    Chapter chapter=new Chapter();
                    BeanUtils.copyProperties(eduChapter,chapter);
                    chapters.add(chapter);
                    List<Video> videos=new ArrayList<>();
                    for (EduVideo eduVideo : eduVideos) {//视频
                        if (eduVideo.getChapterId().equals(eduChapter.getId())){
                            Video video = new Video();
                            BeanUtils.copyProperties(eduVideo,video);
                            videos.add(video);
                        }
                    }
                    chapter.setChildren(videos);

                }
            }
            course.setChildren(chapters);
        }
        return courses;
    }

}
